<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/2/24
 * Time: 11:54
 */

namespace app\console\controller;

use app\common\controller\Console;

class User extends Console {

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index() {
        $q = request()->get('q');
        $where = [];
        if ( $q ) {
            $where['username|money'] = ['like', "%$q%"];
        }
        $model = model('Users');
        $result = $model->order('id desc')->where($where)->paginate(10, false, [
            'query' => request()->get()
        ]);
        $this->assign([
            'result' => $result,
        ]);
        
        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail() {
        $user_id = request()->param("id");

        $user = model("Users")->where("user_id", $user_id)->find();

        $this->assign('user', $user);
        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function followers() {
        $inviter = request()->param("id");
        $status = request()->get('status', null);

        $where = [
            'inviter' => $inviter,
        ];
        if (null !== $status) {
            $where["user_status"] = $status;
        }
        $model = model("Users");
        
        $user = $model->where("user_id", $inviter)->find();

        $result = $model->where($where)->paginate(15,false,[
            'query' => request()->get()
        ]);
        $count = [
            "all" => $model->where(["inviter" => $inviter])->count(),
//            "pending" => $model->where(["user_status" => 0, "inviter" => $inviter])->count("user_id"),
            "enabled" => $model->where(["user_status" => 1, "inviter" => $inviter])->count("user_id"),
            "disabled" => $model->where(["user_status" => 2, "inviter" => $inviter])->count("user_id"),
        ];

        $this->assign([
            'user' => $user,
            'result' => $result,
            'count' => $count,
        ]);
        return $this->fetch();
    }

    public function change($id, $target_status)
    {
        if (!in_array($target_status, [1, 2])) {
            $this->error('错误请求');
        }
        $model = model('Users');
        $info = $model->lock(true)->where('user_id', $id)->find();
        if (!$info) {
            $this->error('未找到用户信息');
        }
        $model->where('user_id', $id)->update(['user_status' => $target_status]);
        return $this->redirect($_SERVER['HTTP_REFERER']);
    }
    
    public function edit($id)
    {
        $info = model('Users')->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function update()
    {
        if ($data = request()->post()) {
            if (!$data['id']) {
                abort(404, '错误请求');
            }
            $update = [
                'username' => $data['username'],
                'money'  => $data['money'],
                'status' => $data['status']
            ];
            if ($data['password'] != $data['old_password']) {
                $update ['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            }
            model('Users')->save($update, ['id' => $data['id']]);
            $this->success('更新成功', url('index'));
        } else {
            abort(404, '错误请求');
        }
    }
}
